/**
 * 
 */
package ru.olympus.antonyhands.ao.server.item;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import ru.olympus.antonyhands.ao.server.db.IPersistentClass;
import ru.olympus.antonyhands.ao.server.item.util.ItemQuantityPairToItemTransformer;
import ru.olympus.antonyhands.ao.server.util.GoogleIterable;

/**
 * @author AntonyHands
 * @since 14.02.2012
 */
public abstract class ItemExchangeBase implements IPersistentClass
{

    private List<ItemQuantityPair> inputItems;
    private ItemQuantityPair result;

    protected ItemExchangeBase()
    {
        // for persistence sake
    }

    protected ItemExchangeBase(ItemQuantityPair result, List<ItemQuantityPair> inputItems)
    {
        this.result = result;
        this.inputItems = inputItems;
    }

    @Override
    public Collection<IPersistentClass> getPersistentChildren()
    {
        List<IPersistentClass> list = new ArrayList<IPersistentClass>();
        list.add(result);
        list.add(new ItemQuantityPairCollection(inputItems));
        return list;
    }

    @Override
    public void setPersistentChildren(List<IPersistentClass> persistentChildren)
    {
        for (IPersistentClass persistent : persistentChildren)
            if (persistent instanceof ItemQuantityPair)
                result = (ItemQuantityPair)persistent;
            else if (persistent instanceof ItemQuantityPairCollection)
                inputItems = ((ItemQuantityPairCollection)persistent).listPairs();
    }

    public Item getResultItem()
    {
        return result.getKey();
    }

    public List<Item> listSourceItems()
    {
        return GoogleIterable.with(inputItems).transform(new ItemQuantityPairToItemTransformer()).toArrayList();
    }
    
    public ItemQuantityPair getResultItemData()
    {
    	return result;
    }

    public List<ItemQuantityPair> listInputItemsData()
    {
    	return inputItems;
    }
}
